$(function(){
    $("body").on('click','[data-stop]',function (e) {e.stopPropagation();});
    /**初始化标签得内容**/
    $.post(ctx+"saleConfig/selectSaletag",{},function (r) {
        if (r.code == 0){
            var data = r.msg;
            $.each(data,function (index,val) {
                var pid = val.parent.tagUuid;
                var div =  $(' <div class="text-left border-bottom-1" pid="'+pid+'" typeid="1"></div>');
                var fistdiv  ='<div class="border-bottom-1 p-t-3"><div class="checkbox m-0 "><span class="m-r-15 m-l-5">'+val.parent.tagGroupName+'</span>' +
                    '<select class="width-200 form-control  inline-block m-b-3" onchange="selectLabelTypeadd(this)" ><option value="1">保持本组原有得标签</option>' +
                    '<option value="2">不添加本组标签,删除原有标签</option><option value="3">使用新的标签替换本组标签</option></select>' +
                    '<label><input type="hidden" value='+val.parent.tagSelect+' /></label></div></div>';
                div.append(fistdiv);
                var cc = $("<div class='m-t-5 p-3 text-left'><input type='hidden' value="+val.parent.tagSelect+" /></div>");
                $.each(val.children,function (i,v) {
                    cc.append('<button type="button" disabled onclick="selectTagIscheckAdd(this)" data-id="'+v.tagUuids+'" ' +
                        'class="btn btn-xs btn-link width-100 m-l-5 p-2 m-b-5 f-s-12 border-solid-g">'+v.tagName+'</button>');
                });
                div.append(cc);
                $("#selectTagAdd").append(div);
            });
        }else{
            $JuXue.n_danger(r.msg);
        }
    });
});

/**保存信息**/
function saveBigEmpInfo(btn) {
    $(btn).attr("disabled","disabled");
    var myCusName = $("#myCusName").val();
    if (myCusName == ""){$JuXue.n_danger("客户姓名不能为空");$(btn).removeAttrs("disabled");return }
    //增加标签
    var form_data = $("#add_myEmp_form").serialize();
    $.post(ctx+"PerSonCustomer/addMyCustomer",form_data,function (r) {
        if (r.code === 0) {
            var data = r.data;
            if(r.msg=="客户重复" || r.msg == "myself"){
                $JuXue.n_danger("该客户已存在");
            }else{
                $("#subcenterModal").empty().modal("hide");
                $JuXue.n_success(r.msg);
            }
        } else {
            $JuXue.n_danger(r.msg);
        }
    });
}

/**切换选择类型**/
function selectLabelTypeadd(btn) {
    var option = $(btn).val();
    if (option == "1" ||option =="2"){
        var allbutton = $(btn).parent().parent().next();
        allbutton.find("button").each(function () {
            $(this).attr("disabled","disabled");
            $(this).removeClass("btn-primary");
        });
    }else {
        var allbutton = $(btn).parent().parent().next();
        allbutton.find("button").each(function () {
            $(this).removeAttrs("disabled");
        });
    }
    $(btn).parent().parent().parent().attr("typeid",option);
}
/**选择标签**/
function selectTagIscheckAdd(btn) {
    var $parentcheck  = $(btn).parent();
    var isMultiInput = $parentcheck.children("input:first-child").val();
    if (isMultiInput == "1"){
        //多选
        var ishave = $(btn).hasClass("btn-primary");
        if (ishave) {
            $(btn).removeClass("btn-primary");
        }else{
            $(btn).addClass("btn-primary");
        }
    }else{
        //单选
        var ishave = $(btn).hasClass("btn-primary");
        if (ishave) {
            $parentcheck.find("button").each(function () {
                $(this).removeClass("btn-primary");
            });
        }else{
            $parentcheck.find("button").each(function () {
                $(this).removeClass("btn-primary");
            });
            $(btn).addClass("btn-primary");
        }
    }
}

/**确定标签**/
function labelSelectSureAdd() {
    var div = $("#selectTagAdd").children("div");
    //包含
    var selectids = new Array();
    var selectnames = new Array();
    $.each(div,function (index,val) {
        var btn = $(this).find("button");
        $.each(btn,function () {
            var isSelect = $(this).hasClass("btn-primary");
            if (isSelect){
                var selectid = $(this).attr("data-id");
                var selectname = $(this).text();
                selectids.push(selectid);selectnames.push(selectname);
            }
        });
    });
    var length = selectids.length;
    if(length > 10){$JuXue.n_danger("标签选择不能大于10个");return ;}
    $("#drownSelectAdd").empty();
    $.each(selectnames,function (i,v) {
        $("#drownSelectAdd").append('<a type="button" class="btn btn-link m-5 p-3 f-s-10 btn-primary ">'+v+'</a>');
    });
    $("#myCusLabel").val(selectids.join(","));
    $("#labelselectadd").removeClass("open");
}
